package com.lottery.ruok.lottrey.activity;


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.RelativeLayout;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.PayPostResponse;
import com.lottery.ruok.lottrey.ui.component.ThreePayInfoView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import glideHelp.GlideHelper;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;
import ui.NavigateBar;
import ui.SquareImageView;
import utils.Utils;
/*
* 充值
* */
public class ChargeThreeActivity extends BaseActivity {
    public static final String EXTRA_MONEY = "EXTRA_MONEY";
    public static final String EXTRA_DATA = "EXTRA_DATA";
    public static final String EXTRA_TITLE = "EXTRA_TITLE";
    @BindView(R.id.navigateBar)
    NavigateBar navigateBar;
    @BindView(R.id.view_charge_info)
    ThreePayInfoView view_charge_info;
    @BindView(R.id.qrcode)
    SquareImageView qrcode;
    @BindView(R.id.charge_now)
    RelativeLayout chargeNow;

    private String imgUrl;//二维码地址
    private boolean isChargeButtonClick = false;
    private String money;
    private PayPostResponse data;
    private String title;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        setContentView(R.layout.activity_charge_threepay);
        ButterKnife.bind(this);
        initView();
    }

    private void initData() {
        money = getIntent().getStringExtra(EXTRA_MONEY);
        title = getIntent().getStringExtra(EXTRA_TITLE);
        data = (PayPostResponse) getIntent().getSerializableExtra(EXTRA_DATA);
    }

    private void initView() {
        if (!Utils.isEmpty(title)) {

            navigateBar.setCenterTitle(title);
        }
        navigateBar.setOnOperateClickListener(new NavigateBar.OnOperateClickListener() {
            @Override
            public void onOperateClick(View view) {
                //如何使用？
                Intent intent = new Intent(ChargeThreeActivity.this, PayChannelExplainActivity.class);
                startActivity(intent);
            }
        });
        if (data.getCode_img_url() != null) {
            GlideHelper.loadImage(ChargeThreeActivity.this, data.getCode_img_url(), qrcode);
        }
        imgUrl = data.getCode_img_url();
        view_charge_info.setMoney(data.getAmount() + "元");
        view_charge_info.setOrderNum(data.getOrdernum());

    }


    @OnClick({R.id.submit, R.id.charge_now})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.charge_now:
                if ("微信扫码支付".equals(getIntent().getStringExtra("title"))) {
//                    sendAnalyticMsg("lottery_" + ChargeActivity.ANALYTIC_VERSION + "_me_recharge_weixin_rechargenow");
                } else if ("支付宝扫码支付".equals(getIntent().getStringExtra("title"))) {
                    //sendAnalyticMsg("lottery_" + ChargeActivity.ANALYTIC_VERSION + "_me_recharge_alipay_rechargenow");
                }

                //把当前图片存储到相册，要多种手机进行测试

                PermissionGen.with(this)
                        .addRequestCode(100)
                        .permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .request();
                break;
            case R.id.submit:
                Utils.showToast("如有充值请点击刷新余额");
                    finish();
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionSuccess(requestCode = 100)
    public void doSomething() {
        if (imgUrl != null) {
            View decorview = getWindow().getDecorView();
            decorview.setDrawingCacheEnabled(true);
            Bitmap Bmp = decorview.getDrawingCache();
            MediaStore.Images.Media.insertImage(getContentResolver(), Bmp, System.currentTimeMillis() + "", System.currentTimeMillis() + "");
            Utils.showToast("截屏已保存到本地相册中");
        }
    }

    @PermissionFail(requestCode = 100)
    public void doFailSomething() {

    }


    public static void startActivity(Activity activity, PayPostResponse data, String money, String title) {
        Intent intent = new Intent(activity, ChargeThreeActivity.class);
        intent.putExtra(EXTRA_MONEY, money);
        intent.putExtra(EXTRA_DATA, data);
        intent.putExtra(EXTRA_TITLE, title);
        activity.startActivity(intent);
    }
}
